package com.atguigu03.threadsafe.runnablesafe;



class SaleTickets1 implements Runnable {

    int tickets = 100;

    boolean flag = true;
    @Override
    public void run() {
        while (flag) {
            show();

        }
    }

    public synchronized void show() {

        if(tickets > 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" + tickets);
            tickets--;
        } else {
            flag = false;
        }

    }
}

public class TicketsTest1 {

    public static void main(String[] args) {


        SaleTickets1 saleTickets = new SaleTickets1();
        Thread thread = new Thread(saleTickets);
        Thread thread1 = new Thread(saleTickets);
        Thread thread2 = new Thread(saleTickets);
        thread.start();
        thread1.start();
        thread2.start();
    }
}
